﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SharedLib.WinFormMVCFramework;
using Tracy.BasicServices;
using System.IO;

namespace TracyGUI.View
{
    public partial class FormPreferences : WinFormViewBase, IPreferencesView
    {
        public FormPreferences()
        {
            InitializeComponent();
        }

        UserPreferencesManager.UserPreferences _currentPref = null;
        public void LoadUserPreferences(UserPreferencesManager.UserPreferences pref)
        {
            InvokeOnUIThread(() =>
            {
                _currentPref = pref;
                if (pref.FirstUseFlag)
                {
                    MessageBox.Show(Tracy.TracyFacade.Instance.LanguageManager.GetText("MessageBox_PreferencesView_FirstUse_Message"));
                    buttonCancel.Enabled = false;
                }
                textBoxDefaultPath.Text = pref.SavePath;
                textBoxTempPath.Text = pref.TempPath;
                buttonOK.Select();
                folderBrowserDialogDefaultPath.SelectedPath = textBoxDefaultPath.Text;
                folderBrowserDialogTempPath.SelectedPath = textBoxTempPath.Text;

            });

        }

        public event EventHandler<SharedLib.DataChangedEventArgs<Tracy.BasicServices.UserPreferencesManager.UserPreferences>> UserPreferencesChanging;

        private bool CheckPath(string path)
        {
            if (String.IsNullOrEmpty(path))
            {
                MessageBox.Show(Tracy.TracyFacade.Instance.LanguageManager.GetText("MessageBox_PreferencesView_PathEmpty_Message"));
                return false;
            }
            if (!Directory.Exists(path))
            {
                try
                {
                    Directory.CreateDirectory(path);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(Tracy.TracyFacade.Instance.LanguageManager.GetText("MessageBox_PreferencesView_PathInvalid_Message") + ex.Message);
                    return false;
                }
            }
            return true;
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (!CheckPath(textBoxTempPath.Text) || !CheckPath(textBoxDefaultPath.Text)) return;

            UserPreferencesManager.UserPreferences newPref = _currentPref.Clone() as UserPreferencesManager.UserPreferences;
            newPref.SavePath = textBoxDefaultPath.Text;
            newPref.TempPath = textBoxTempPath.Text;
            UserPreferencesChanging.Invoke(this, new SharedLib.DataChangedEventArgs<UserPreferencesManager.UserPreferences>(_currentPref, newPref));
            DialogResult = System.Windows.Forms.DialogResult.OK;
            Close();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
            Close();
        }

        private void buttonDefaultPathBrowse_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialogDefaultPath.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                textBoxDefaultPath.Text = folderBrowserDialogDefaultPath.SelectedPath;
            }
        }
        private void buttonTempPathBrowse_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialogTempPath.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                textBoxTempPath.Text = folderBrowserDialogTempPath.SelectedPath;
            }
        }

        private void FormPreferences_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (_currentPref.FirstUseFlag && DialogResult != System.Windows.Forms.DialogResult.OK)
            {
                e.Cancel = true;
            }
        }

        
    }
}
